package com.dcliment.jukebox.display.gui
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	/**
	 * All buttons have a movieclip in on the timeline with a linkage of mBkgd
	 *  - OFF and ON states are denoted by the first and second frames respectively
	 *  
	 * @author dcliment
	 * 
	 */	
	
	public class SimpleNavButton extends MovieClip
	{
		
		// Display
		private var _label:TextField;
		private var _onStateTF:TextFormat;
		private var _offStateTF:TextFormat;
		private var _css:StyleSheet;
		private var _twoLineHeight:Number;
		// Accessors
		private var _selected:Boolean;
		private var _type:String;
		// Specific to the chapter nav buttons (might want to seperate this out but not really nessessary yet 
		private var _id:int;
		private var _token:String;
		
		public function SimpleNavButton()
		{
			
			// --------------------------- >>
			//STYLE SHEET ~~ allows for superScript and SubScrupt
			_css = new StyleSheet();
			_css.parseCSS('sub{font-family:"GG Subscript";display:run-in;}sup{font-family:"GG Superscript";display:run-in;}');
			
			_label 	= this["mLabel"];
			if(!_label)
				throw new Error("SimpleNavButton ERROR :: the flash library item is missing a textfield with the linkage name 'mLabel'");
			
			_label.mouseEnabled = false;
			_offStateTF = _label.getTextFormat(0,1);
			
			_label.styleSheet = _css;
			
			// ---------------------
			// get proper labeling size
			_label.htmlText = "line 1 <br> line 2"; // this gives me a baseline for waht the height should be for a two line chapter button
			_twoLineHeight = _label.textHeight; // grab the size
			
			_label.visible = false; // only visible = true when label is set
			
			enable();
		}
		
		
		//
		// PUblic functions
		//
		public function enable():void
		{
			
			this.addEventListener(MouseEvent.CLICK, onClick, false, 0, true);
			//this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver, false, 0, true);
			//this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut, false, 0, true);
			
			this.selected = false;
			this.gotoAndStop(1);
			this.mouseEnabled = true;
			this.buttonMode = true;
			
			if(_onStateTF)
			{
				_label.styleSheet = null;
				_label.setTextFormat(_offStateTF);
				_label.styleSheet = _css;
			}
			
		}
		public function disable():void
		{	
			this.removeEventListener(MouseEvent.CLICK, onClick);
			//this.removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			//this.removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			
			this.mouseEnabled = false;
			this.buttonMode = false;
		}
		public function onState():void
		{
			this.disable();
			this.selected = true;
			
			this.gotoAndStop(2);
			
			if(_onStateTF)
			{
				_label.styleSheet = null;
				_label.setTextFormat(_onStateTF);
				//_label.styleSheet = _css; // so when i set the css here the textFOrmat fails.. but when i put the stylesheet above it does work. hmm?
			}
		}
			
		//
		// Private Functions
		//
		protected function onClick(e:MouseEvent):void
		{
			onState();
		}
		protected function onMouseOver(e:MouseEvent):void
		{
			this.gotoAndStop(2);
		}
		protected function onMouseOut(e:MouseEvent):void
		{
			this.gotoAndStop(1);	
		}
		
		//
		// ACCESSORS
		//
		public function get selected():Boolean
		{
			return _selected;
		}

		public function set selected(value:Boolean):void
		{
			_selected = value;
		}
		
		public function set label($text:String):void
		{	

			/// DC :: this is really how it shoudl be done but no need to change it if this is working for you\\\
			
			var h:Number = this.height; // key is to get the height of the piece before you add the text that could make the textfield bigger than the whole.
			
			_label.htmlText = $text;
			_label.autoSize = TextFieldAutoSize.CENTER;
			
			
			_label.y = (h / 2) - (_label.textHeight / 2);
			
			_label.height = _label.textHeight;
			_label.visible = true;
			
			
		}

		public function get id():int
		{
			return _id;
		}

		public function set id(value:int):void
		{
			_id = value;
		}

		public function get type():String
		{
			return _type;
		}

		public function set type(value:String):void
		{
			_type = value;
		}

		public function get token():String
		{
			return _token;
		}

		public function set token(value:String):void
		{
			_token = value;
		}

		public function get onStateTF():TextFormat
		{
			return _onStateTF;
		}

		public function set onStateTF(value:TextFormat):void
		{
			_onStateTF = value;
		}

			
	}
}